
<template>
  <div>
    <div style="margin-bottom: 10px;">
      <el-input v-model="level" placeholder="请输入等级..."  suffix-icon="el-icon-search" style="width: 200px"></el-input>
      <el-input v-model="name" placeholder="请输入姓名..."  suffix-icon="el-icon-search" style="width: 200px;margin-left: 5px"></el-input>
      <el-button style="margin-left: 5px;" @click="loadPostSearch">查询</el-button>
      <el-button @click="resetParam">重置</el-button>
      <el-button style="margin-left: 5px;" @click="add">新增</el-button>
    </div>

    <el-table :data="tableData"
              :header-cell-style="{background:'#eaecf3',color:'rgb(106,117,119)'}" border
    >
      <el-table-column prop="therapistId" label="技师ID" width="120">
      </el-table-column>
      <el-table-column prop="name" label="技师名称" width="120">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="100">
      </el-table-column>
      <el-table-column prop="level" label="等级" width="130">
      </el-table-column>
      <el-table-column prop="hireDate" label="入职日期" width="130">
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" width="140">
      </el-table-column>
      <el-table-column prop="specialty" label="专长" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80">
      </el-table-column>
      <el-table-column prop="commissionRate" label="提成比例" width="80">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="260" >
        <template slot-scope="scope">
          <el-button @click="mod(scope.row)">编辑</el-button>
          <el-button @click="del(scope.row.therapistId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-col :span="20">
            <el-input v-model="form.level"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-col :span="20">
            <el-input v-model="form.phone"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="入职时间" prop="hireDate">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.hireDate" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="专长" prop="specialty">
          <el-col :span="20">
            <el-input v-model="form.specialty"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="ACTIVE"></el-radio>
            <el-radio label="ON_LEAVE"></el-radio>
            <el-radio label="RESIGNED"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提成比例" prop="commissionRate">
          <el-col :span="20">
            <el-input v-model="form.commissionRate"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  name: 'TherapistManage',

  data() {
    return {
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      name:'',
      level:'',
      centerDialogVisible: false,
      form: {
        therapistId: '',
        name: '',
        gender: '',
        level: '',
        hireDate: '',
        phone: '',
        specialty: '',
        status: '',
        commissionRate: ''
      }
    }
  },
  methods:{
    resetForm() {
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.form=row;
      });
    },
    add() {
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    doSave() {
      this.$axios.post(this.$httpUrl + '/therapist/save',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    doMod(){
      this.$axios.post(this.$httpUrl + '/therapist/update',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    save(){
      this.$refs.form.validate((valid)=>{
        if(valid){
          if(this.form.therapistId){
            this.doMod();
          }else {
            this.doSave();
          }
        }else {
          console.log('error submit');
          return false;
        }
      });
    },
    del(therapistId) {
      console.log(therapistId);

      this.$axios.get(this.$httpUrl + '/therapist/delete?therapistId=' + therapistId)
          .then(res => res.data)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                message: '操作成功！',
                type: 'success'
              });
              this.loadPost();
            } else {
              this.$message({
                message: '操作失败！',
                type: 'error'
              });
            }
          })
          .catch(error => {
            console.error('删除操作出错:', error);
            this.$message({
              message: '操作出错！',
              type: 'error'
            });
          });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/therapist/list').then(res=>res
          .data).then(res=>{
        console.log(res)
      })
    },
    loadPost(){
      this.$axios.get(this.$httpUrl+'/therapist/listPage',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    loadPostSearch(){
      this.$axios.get(this.$httpUrl+'/therapist/listPageSearch',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name:this.name,
          level:this.level
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    resetParam(){
      this.name='';
      this.level='';
      this.loadPost();
    }
  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();
  }
}
</script>
<style scoped>

</style>